package com.mobi.Survey.lib.activities;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.mobi.Survey.lib.video.GenericPlayer;
import com.mobi.Survey.lib.video.IVideoCallbacks;
import com.mobi.Survey.lib.video.VideoPlayer;

/**
 * 
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * VideoScreen.java This class is responsible displaying Video for the product
 * launch this screen with url to the video and type of video i.e youtube or
 * general video on remote server if video with that url already exist on mobile
 * storage drive that video will be played
 */
public class VideoScreen extends AbstractScreen implements IVideoCallbacks
{

	private VideoPlayer vp;
	private String url;


	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		hideTitle = true;

		super.onCreate(savedInstanceState);

		Intent intent = getIntent();

		if (intent != null)
		{
			Bundle bundle = intent.getExtras();
			if (bundle != null)
			{
				String data = (String) bundle.get("data");
			
				url = data.substring(1, data.length());

				vp = new GenericPlayer(this);
				if (url != null && vp != null)
				{

					vp.setUpPlayer(url);
					vp.startPlaying();
					vp.setVideoCallbackListener(this);
					View v = vp.setupView(2);

					vp.setMessage(Messages.BUFFERING);
					setContentView(v);
				} else
				{
					finish();
				}

			}

		}

	}

	@Override
	public void onBackPressed()
	{

		super.onBackPressed();
		finish();
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{

		return super.onCreateDialog(id);

	}

	@Override
	protected void initUiVars()
	{

	}

	@Override
	protected int getLayoutId()
	{

		return 0;
	}

	// private boolean cancel;

	@Override
	protected void onRelease()
	{
		printMessage("Stopping Video and Releasing Resources");
		// cancel = true;
		// if (task != null)
		// task.cancel(true);

		vp.stopPlaying();
		vp = null;
	}

	/**
	 * if mobile unable to play youtube video app will try to download video of
	 * media format 18 to
	 * 
	 * @author Yahya Arshad
	 * 
	 */
	// private class YoutubeDownloadTask extends
	// AsyncTask<String, Integer, Object> {
	//
	// String videoPath;
	// String msg;
	//
	// protected Object doInBackground(String... params) {
	// if (cancel)
	// return null;
	// // get youtube id from youtube url
	// String yId = YouTubeUtility.getVideoIdFromUrl(params[0]);
	//
	// // calculate hash value
	// int hash = Helper.getHashNameFromUrl(yId);
	// String tempFile = Helper.getTempFile(hash + "");
	// if (!new File(tempFile).exists())
	// try {
	// if (!Device.isSdPresentForReadWrite()
	// && Device.getAvailableSDSpace() > 50) {
	// msg = Messages.LESS_SPACE_50MB;
	// return null;
	//
	// }
	// videoPath = YouTubeUtility.downloadYoutubeVideo(yId,
	// tempFile);
	//
	// } catch (RuntimeException ex) {
	// msg = ex.getMessage();
	// } catch (Exception e) {
	// msg = e.getMessage();
	// }
	//
	// else {
	// videoPath = tempFile;
	// }
	//
	// return null;
	//
	// }
	//
	// protected void onPostExecute(Object result) {
	//
	// super.onPostExecute(result);
	// if (cancel) {
	//
	// return;
	// }
	// if (videoPath != null && msg == null && !cancel) {
	// vp.showMessage();
	//
	// vp.setMessage(Messages.PLAYING_VIDEO);
	//
	// vp.setUpPlayer(videoPath);
	// vp.startPlaying();
	// } else {
	// showLongToast(msg);
	// vp.dismissTextMessage();
	// vp.dismissProgress();
	// finish();
	//
	// }
	// }
	//
	// protected void onPreExecute() {
	//
	// vp.setMessage(Messages.DOWNLOADING_VIDEO);
	// // vp.showMessage();
	//
	// vp.showProgress();
	//
	// super.onPreExecute();
	//
	// }
	//
	// }

	// YoutubeDownloadTask task;

	protected void onBtnDownloadVideo()
	{
		// if (VideoPlayer.TYPE_YOUTUBE == type) {
		// task = new YoutubeDownloadTask();
		// task.execute(url);
		// } else {
		// showLongToast(Messages.UNABLE_TOP_PLAY_GENERIC_VIDEO + url);
		// finish();
		// }
	}

	@Override
	public void onPrepare()
	{

	}

}
